﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DigitTransform
{
    public partial class Form1 : Form
    {
        #region Struct

        protected struct Binary
        {
            public static string MaxValue;
            public static string MinValue;
        }

        protected struct Octonary
        {
            public static string MaxValue;
            public static string MinValue;
        }

        protected struct Integer64
        {
            public static string MaxValue;
            public static string MinValue;
        }

        protected struct Hexadecimal
        {
            public static string MaxValue;
            public static string MinValue;
        }

        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitStruct();
            InitComboBox();
        }

        private void InitComboBox()
        {
            this.cBSource.Items.Add("2");
            this.cBSource.Items.Add("8");
            this.cBSource.Items.Add("10");
            this.cBSource.Items.Add("16");
            this.cBSource.SelectedIndex = 2;

            this.cBDest.Items.Add("2");
            this.cBDest.Items.Add("8");
            this.cBDest.Items.Add("10");
            this.cBDest.Items.Add("16");
            this.cBDest.SelectedIndex = 0;
        }

        private void InitStruct()
        {
            Binary.MaxValue = Convert.ToString(Int64.MaxValue, 2);
            Binary.MinValue = Convert.ToString(Int64.MinValue, 2);

            Octonary.MaxValue = Convert.ToString(Int64.MaxValue, 8);
            Octonary.MinValue = Convert.ToString(Int64.MinValue, 8);

            Integer64.MaxValue = Int64.MaxValue.ToString();
            Integer64.MinValue = Int64.MinValue.ToString();

            Hexadecimal.MaxValue = Convert.ToString(Int64.MaxValue, 16);
            Hexadecimal.MinValue = Convert.ToString(Int64.MinValue, 16);
        }

        private void btn_trans_Click(object sender, EventArgs e)
        {
            //验证输入类型
            string istring=this.tbSource.Text;
            if (istring.Length <= 0)
            {
                return;
            }            

            try
            {
                Int64 t = Convert.ToInt64(this.tbSource.Text, Convert.ToInt32(this.cBSource.SelectedItem));
                this.tbDest.Text = Convert.ToString(t, Convert.ToInt32(this.cBDest.SelectedItem)).ToUpper();
            }
            catch (OverflowException)
            {
                switch (this.cBSource.SelectedItem.ToString())
                {
                    case "2":
                        MessageBox.Show("二进制可转换范围：" + Binary.MinValue + " ～ " + Binary.MaxValue);
                        break;
                    case "8":
                        MessageBox.Show("八进制可转换范围：" + Octonary.MinValue + " ～ " + Octonary.MaxValue);
                        break;
                    case "10":
                        MessageBox.Show("十进制可转换范围：" + Integer64.MinValue + " ～ " + Integer64.MaxValue);
                        break;
                    case "16":
                        this.tbSource.MaxLength = 8;
                        MessageBox.Show("十六进制可转换范围：" + Hexadecimal.MinValue + " ～ " + Hexadecimal.MaxValue);
                        break;
                    default:
                        break;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void cBSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.cBSource.SelectedItem.ToString())
            {
                case "2":
                    this.tbSource.MaxLength = Binary.MinValue.Length;
                    break;
                case "8":
                    this.tbSource.MaxLength = Octonary.MinValue.Length;
                    break;
                case "10":
                    this.tbSource.MaxLength = Integer64.MinValue.Length;
                    break;
                case "16":
                    this.tbSource.MaxLength = Hexadecimal.MinValue.Length;
                    break;
                default:
                    break;
            }
        }
    }
}
